package com.ll.micropoint.point.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.common.core.entity.PointInfo;
import com.ll.common.core.entity.PointRule;
import com.ll.common.redis.utils.RedisUtils;
import com.ll.micropoint.point.mapper.PointInfoMapper;
import com.ll.micropoint.point.mapper.PointRuleMapper;
import com.ll.micropoint.point.service.IPointRuleService;
import com.ll.micropoint.point.service.IPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
public class PointServiceImpl implements IPointService {

    @Autowired
    private PointInfoMapper pointInfoMapper;

    @Autowired
    private PointRuleMapper pointRuleMapper;

    @Autowired
    private IPointRuleService pointRuleService;

    @Autowired
    private RedisUtils redisUtils;
    @Override
    public void add(Integer uid, Integer pointType) {

//        QueryWrapper<PointRule> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("point_type",pointType);
//        PointRule pointRule = pointRuleMapper.selectOne(queryWrapper);

        //从redis中获取积分规则
        List<PointRule> pointlist = pointRuleService.pointlist();
        PointRule pointRule = pointlist.stream().filter(rule ->
                rule.getPointType().equals(pointType)
        ).findFirst().orElse(null);

        if (!ObjectUtils.isEmpty(pointRule)){
            PointInfo pointInfo = new PointInfo();
            pointInfo.setUid(uid);
            pointInfo.setPointType(pointType);
            pointInfo.setPoint(pointRule.getPoint());
            pointInfoMapper.insert(pointInfo);
        }
    }
}
